<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_findTSPPathsService"></title>
    <style>
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        text-align: center;
        z-index: 1000;
        border-radius: 4px;
      }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="toolbar" class="panel panel-primary">
  <div class='panel-heading'>
      <h5 class='panel-title text-center' data-i18n="resources.text_findTSPPaths"></h5>
  </div>
  <div class='panel-body content'>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_sites" onclick="selectPoints()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_check" onclick="findTSPPathsProcess()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearElements(true)"/>
  </div>
</div>
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
<script type="text/javascript">
    var map, draw, source, nodeArray = [], resultLayer,
        baseUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    //多旅行商分析是无序的路径分析
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        code:'',
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
            controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
            view: new ol.View({
                center: [5000, -3700],
                zoom: 2,
                projection: projection,
                multiWorld: true
            })
        });
        var options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        source = new ol.source.Vector();
        var vectorLayer = new ol.layer.Vector({
            source: source
        });
        map.addLayer(layer);
        map.addLayer(vectorLayer);
    });

    function selectPoints() {
      clearElements(true);
      // 添加站点
      draw = new ol.interaction.Draw({
        source: source,
        type: 'Point'
      });
      draw.on('drawend', function(e) {
        var feature = e.feature;
        if (feature) {
          // 设置点的样式
          var iconStyle = new ol.style.Style({
            image: new ol.style.Icon({
              src: '../img/marker.png'
            }),
          });
          feature.setStyle(iconStyle);
          nodeArray.push(feature.getGeometry());
        }
      });
      map.addInteraction(draw);
    }

    function findTSPPathsProcess() {
        if (nodeArray.length < 2) {
          return widgets.alert.showAlert(resources.msg_check, false);
        }
        clearElements();

        //创建多旅行商分析参数实例
        var resultSetting = new ol.supermap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new ol.supermap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        var findTSPPathsParameter = new ol.supermap.FindTSPPathsParameters({
            //是否指定终止点
            endNodeAssigned: false,
            isAnalyzeById: false,
            //旅行商分析途经点数组，必设字段
            nodes: nodeArray,
            parameter: analystParameter
        });
        //进行查找
        new ol.supermap.NetworkAnalystService(serviceUrl).findTSPPaths(findTSPPathsParameter).then(function (serviceResult) {
            serviceResult.result.tspPathList.map(function (result) {
                var vectorSource = new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(result.route)
                });
                resultLayer = new ol.layer.Vector({
                    source: vectorSource,
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'rgba(100, 100, 225, 10)',
                            width: 3
                        }),
                        fill: new ol.style.Fill({
                            color: 'rgba(0, 0, 255, 0.1)'
                        })
                    })
                });
                map.addLayer(resultLayer);
            });
        });
    }

    function clearElements(clear) {
      draw && map.removeInteraction(draw);
      map.removeLayer(resultLayer);
      pathLayerArray = [];
      if (clear) {
        nodeArray = [];
        source.clear();
      }
    }
</script>
</body>
</html>